package com.irdstudio.efp.esb.api.rest.zzd;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.irdstudio.efp.esb.api.bean.zzd.ReqPlatformQuotaBean;
import com.irdstudio.efp.esb.api.bean.zzd.RespPlatformQuotaBean;
import com.irdstudio.efp.esb.api.rest.EsbServerController;
import com.irdstudio.efp.esb.common.constant.EsbBizEnums;
import com.irdstudio.efp.esb.service.bo.req.hj.ReqProductLoanQueryBean;
import com.irdstudio.efp.esb.service.facade.hj.ProductLoanQueryService;
import com.irdstudio.basic.framework.core.exception.BizException;
import com.irdstudio.basic.framework.core.exception.ESBException;
import com.irdstudio.basic.framework.core.util.InterfaceCheckUtils;
import com.irdstudio.basic.framework.core.util.StringUtil;

public class PlatformQuotaController implements EsbServerController {

	private static Logger logger = LoggerFactory.getLogger(PlatformQuotaController.class);
	@Autowired
	@Qualifier("productLoanQueryService")
	private ProductLoanQueryService productLoanQueryService;

	/**
	 * 平台额度信息查询接口逻辑处理方法，处理逻辑包括：<br>
	 * 1.将用户借款请求报文体对象转换成用户借款请求对应的domain；<br>
	 * 2.对用户借款请求domain中值做是否必输性校验；<br>
	 * 3.对用户借款请求domain中值做是否合规性校验；<br>
	 * 4.调用对应的中台服务；<br>
	 * 
	 * @param esbReqBodyBean
	 * @return
	 * @throws Exception
	 * @author liyakui
	 * @since 2018年12月18日 下午5:16:37
	 * @version 1.0
	 */
	@Override
	public String execute(String esbReqBodyBean) throws ESBException, Exception {
		ReqPlatformQuotaBean reqPlatformQuotaBean = null;
		RespPlatformQuotaBean esbRespBodyBean = new RespPlatformQuotaBean();
		ReqProductLoanQueryBean reqProductLoanQueryBean = new ReqProductLoanQueryBean();
		logger.info("平台额度信息查询交易处理开始!");
		try {
			// 1.转换请求bean
			reqPlatformQuotaBean = JSON.toJavaObject(JSONObject.parseObject(esbReqBodyBean), ReqPlatformQuotaBean.class);

			// 2.必输项校验
			String bankName = (String)InterfaceCheckUtils.getValue("bankName","银行类型",reqPlatformQuotaBean.getBankName(),true);

			// 3.调用中台服务：前往互金查询平台可用额度
			reqProductLoanQueryBean.setProdType("");// 产品类型
			reqProductLoanQueryBean.setChannelType("");// 渠道类型
			productLoanQueryService.productLoanQuery(reqProductLoanQueryBean);
			// 4.封装返回报文
			esbRespBodyBean.setBankName(bankName);
			esbRespBodyBean.setBankRetcode(EsbBizEnums.RetCodeEnum.SUCCESS.VALUE);
			esbRespBodyBean.setBankRetmsg(EsbBizEnums.RetCodeMsg.SUCCESS_MSG.VALUE);
			esbRespBodyBean.setRetcode(new Integer(EsbBizEnums.RetCodeEnum.SUCCESS.VALUE));
			esbRespBodyBean.setRetmsg(EsbBizEnums.RetCodeMsg.SUCCESS_MSG.VALUE);
			esbRespBodyBean.setLoanQuotaLeft(new Integer(""));
			esbRespBodyBean.setQuotaModifyTime("");
			esbRespBodyBean.setTotalLoanQuota(new Integer(""));
			throw new ESBException();
		} catch (ESBException e) {
			logger.error("平台额度接口调用查询失败！");
		} catch (Exception e) {
			logger.error("平台额度接口调用查询失败！");
		}
		return JSON.toJSON(esbRespBodyBean).toString();
	}

	/**
	 * 必输项校验
	 * 
	 * @param reqBodyMap
	 * @throws BizException
	 * @author liyakui
	 * @since 2018年12月18日 下午8:33:03
	 * @version 1.0
	 */
	public void checkMessageRequired(Map<String, Object> reqBodyMap) throws BizException {
		Map<String, String> requiredMap = new HashMap<String, String>();
		requiredMap.put("sign_type", "签名方式");
		requiredMap.put("service_version", "接口版本");
		requiredMap.put("input_charset", "字符集");
		requiredMap.put("sign", "签名");
		requiredMap.put("loaner_spid", "银行商户号");
		requiredMap.put("file_name", "文件名");
		requiredMap.put("file_abstract", "授权文件摘要的值");
		requiredMap.put("sign_key_index", "密钥序号");
		requiredMap.put("bank_name", "银行类型");

		StringUtil.checkRequiredValue(reqBodyMap, requiredMap);
	}

}
